package com.jtech.together.view.activity;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jtech.together.R;
import com.jtech.together.manager.SettingManager;
import com.jtech.together.model.UserModel;
import com.jtech.together.mvp.contract.UserProfileContract;
import com.jtech.together.mvp.presenter.UserProfilePresenter;
import com.jtech.together.util.ActivityJump;
import com.jtech.together.util.ImageUtils;
import com.jtech.together.util.SexUtil;
import com.jtech.together.util.StatusBarUtils;
import com.jtech.together.util.Util;
import com.jtech.together.view.activity.base.BaseActivity;
import com.jtech.together.view.weight.dialog.CustomProgressDialog;
import com.umeng.analytics.MobclickAgent;

import butterknife.Bind;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;
import jp.wasabeef.blurry.Blurry;

/**
 * 用户信息页面
 * Created by wuxubaiyang on 2017/5/11.
 */
public class UserProfileActivity extends BaseActivity implements UserProfileContract.View, View.OnClickListener {
    public static final String KEY_USER_OBJECT_ID = "keyUserObjectId";

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.content)
    CoordinatorLayout content;
    @Bind(R.id.collapsing_toolbar_user_profile)
    CollapsingToolbarLayout collapsingToolbarLayout;
    @Bind(R.id.imageview_user_profile_avatar)
    ImageView imageViewAvatar;
    @Bind(R.id.fab_user_profile)
    FloatingActionButton fab;
    @Bind(R.id.textview_user_profile_nickname)
    TextView textViewNickname;
    @Bind(R.id.textview_user_profile_sex)
    TextView textViewSex;
    @Bind(R.id.textview_user_profile_age)
    TextView textViewAge;
    @Bind(R.id.textview_user_profile_bio)
    TextView textViewBio;

    private UserProfileContract.Presenter presenter;

    @Override
    protected void initVariables(Bundle bundle) {
        //绑定P类
        presenter = new UserProfilePresenter(getActivity(), this, bundle);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_user_profile);
        //设置透明状态栏
        StatusBarUtils.setWindowStatusBarTransparent(getActivity());
        //设置标题栏
        setSupportActionBar(toolbar);
        //设置fab为不可操作
        fab.setEnabled(false);
        //设置后退键点击事件
        toolbar.setNavigationOnClickListener(this);
        //设置标题颜色
        collapsingToolbarLayout.setExpandedTitleColor(Color.WHITE);
        collapsingToolbarLayout.setCollapsedTitleTextColor(Color.WHITE);
        //设置标题栏颜色
        collapsingToolbarLayout.setContentScrimColor(getResources().getColor(R.color.theme_color_primary));
        //设置状态栏颜色
        collapsingToolbarLayout.setStatusBarScrimColor(getResources().getColor(R.color.theme_color_primary));
    }

    @Override
    protected void loadData() {
        //请求用户信息
        CustomProgressDialog.showProgressDialog(getActivity());
        presenter.loadUserProfile();
        presenter.loadIsFriend();
    }

    /**
     * 设置用户信息
     */
    private void setUserProfile(UserModel userModel) {
        //显示用户头像
        ImageUtils.requestImage(getActivity(), userModel.getAvatar(), new Consumer<Bitmap>() {
            @Override
            public void accept(Bitmap bitmap) throws Exception {
                if (null != bitmap) {
                    Blurry.with(getActivity())
                            .from(bitmap)
                            .into(imageViewAvatar);
                }
            }
        });
        //标题栏设置用户昵称
        collapsingToolbarLayout.setTitle(userModel.getNickname());
        //设置用户昵称
        textViewNickname.setText(userModel.getNickname());
        //设置用户性别
        textViewSex.setText(SexUtil.getSex(userModel.getSex()));
        //设置用户年龄
        textViewAge.setText(Util.getAge(userModel.getBirthday()) + "岁");
        //设置用户简介
        textViewBio.setText(userModel.getInfo());
    }

    @Override
    public void loadSuccess(UserModel userModel) {
        CustomProgressDialog.dismissProgressDialog();
        setUserProfile(userModel);
    }

    @Override
    public void loadFail(String error) {
        CustomProgressDialog.dismissProgressDialog();
        Snackbar.make(content, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void isFriend(boolean isFriend) {
        if (isFriend) {
            fab.setImageResource(R.drawable.ic_user_profile_go_chat);
            fab.setEnabled(true);
        } else {
            if (SettingManager.get(getActivity())
                    .isSendAddFriendMessage(presenter.getObjectId()) || presenter.isMine()) {
                fab.setImageResource(R.drawable.ic_user_profile_no);
                fab.setEnabled(false);
            } else {
                fab.setImageResource(R.drawable.ic_user_profile_add_friend);
                fab.setEnabled(true);
            }
        }
    }

    @Override
    public void isFriendFail(String error) {
        Snackbar.make(content, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void sendInviteSuccess() {
        CustomProgressDialog.dismissProgressDialog();
        Snackbar.make(content, "已发送好友请求，一天内不可重复发送", Snackbar.LENGTH_SHORT).show();
        //设置fab为不可用
        fab.setImageResource(R.drawable.ic_user_profile_no);
        fab.setEnabled(false);
    }

    @Override
    public void sendInviteFail(String error) {
        CustomProgressDialog.dismissProgressDialog();
        Snackbar.make(content, error, Snackbar.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        //后退
        onBackPressed();
    }

    /**
     * fab的点击事件
     */
    @OnClick(R.id.fab_user_profile)
    public void onFabClick() {
        if (presenter.isFriend()) {
            //跳转到聊天页面
            Bundle bundle = new Bundle();
            bundle.putString(ChatActivity.KEY_TARGET_ID, presenter.getObjectId());
            ActivityJump.build(getActivity(), ChatActivity.class)
                    .addBundle(bundle)
                    .jump();
        } else {
            //发送好友邀请
            CustomProgressDialog.showProgressDialog(getActivity(), "正在发送邀请");
            presenter.addFriend(getResources().getString(R.string.add_friend_message));
        }
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(TAG);
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(TAG);
        MobclickAgent.onPause(this);
    }
}